<?php

namespace App\Models\Shop;

use App\Helpers\SelfEncrypt;
use App\Models\BaseModel;
use App\Models\User\AccountFlowModel;
use Carbon\Carbon;
use App\Models\Shop\OrderPaymentModel;
use DC\UserCenter\Models\UserWalletModel;
use Illuminate\Support\Facades\DB;
use App\Models\Bonus\BonusRecordModel;
use DC\BonusCenter\Services\ShoppingBonus;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Yansongda\Pay\Pay;

class OrderModel extends BaseModel
{
    protected $table = 'shop_order';

    protected $primaryKey = 'order_id';

    public $dateFormat = 'U';

    public function goods()
    {
        return $this->hasMany('App\Models\Shop\OrderGoodsModel','order_id','order_id');
    }

    public function store()
    {
        return $this->belongsTo('App\Models\Store\ShopStoreModel','store_id','id');
    }

    public function user()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','uid','member_id');
    }

    public function createOrder($user_id,$address,$remark,$zone = 1)
    {
        $order = new self();
        $order->order_sn = SelfEncrypt::createCode('S');
        $order->uid = $user_id;
        $order->post_address = get_address($address['area']).$address['address'];
        $order->area_address = $address['area'];
        $order->consignee = $address['consignee'];
        $order->phone = $address['phone'];
        $order->pay_status = 0;
        $order->status = 0;
        $order->goods_zone = $zone;
        $order->comment = $remark;

        if($order->save()){
            return $order->order_id;
        }

        return false;
    }

    public function payOrder($order_id,$user_id,$voucher)
    {
        $order = self::where('uid',$user_id)->where('status',0)->find($order_id);

        if(!$order){
            $this->errorMsg ='订单不存在或已支付';
            return false;
        }

        $res = false;

        switch ($order->pay_type){
            case 100:
                $res = true ;
                break;
            case 101:
                $res = $this->payMoney1($user_id,$order);
                break;
            case 102:
                $res = $this->payMoney2($user_id,$order);
                break;
            case 103:
                $res =  $this->payMoney3($user_id,$order);
                break;
        }

        if($res){
            self::where('order_id',$order_id)->update([
                'status'=>1,
                'voucher'=>$voucher,
                'pay_status'=>1,
                'paid_at'=>Carbon::now()->timestamp
            ]);

            $orderPayment = new OrderPaymentModel();
            $orderPayment->where('out_trade_no',$order['order_sn'])->update([
                'pay_status'=>1,
                'pay_at'=>Carbon::now()->timestamp
            ]);

            $this->award($user_id,$order->pay_price,$order->goods_zone,$order->area_address,$order->dan);

            return true;
        }

        return false;
    }


    public function payPcOrder($order_id,$user_id,$pay_type)
    {
        $order = self::where('uid',$user_id)->where('status',0)->find($order_id);
        if(!$order){
            $this->errorMsg ='订单不存在或已支付';
            return false;
        }
        if(!$pay_type || $pay_type =='' || empty($pay_type)){
            $this->errorMsg ='请选择支付方式';
            return false;
        }
        if(!$pay_type == 105){
            $this->errorMsg ='系统暂不支持改支付方式';
            return false;
        }

        $res = false;

        switch ($pay_type){
            case 101:
                $res = $this->payMoney1($user_id,$order);
                break;
            case 102:
                $res = $this->payMoney2($user_id,$order);
                break;
            case 103:
                $res =  $this->payMoney3($user_id,$order);
                break;
            case 104:
                $res =  $this->payMoney4($user_id,$order);
                break;
            default:
                $res = $this->payMoney2($user_id,$order);
        }

        if($res){
            self::where('order_id',$order_id)->update([
                'status'=>1,
                'pay_type'=>$pay_type,
                'pay_status'=>1,
                'paid_at'=>Carbon::now()->timestamp
            ]);

            $orderPayment = new OrderPaymentModel();
            $orderPayment->where('out_trade_no',$order['order_sn'])->update([
                'pay_status'=>1,
                'pay_at'=>Carbon::now()->timestamp
            ]);

            $this->award($user_id,$order->pay_price,$pay_type);

            return true;
        }

        return false;
    }

    public function payMoney1($user_id,$order)
    {

        $wallet = UserWalletModel::find($user_id);
        if($wallet->money1-$order->pay_price<0){
            $this->errorMsg =get_currency()['money1'].'余额不足';
            return false;
        }

        $data['money1'] = DB::raw('`money1`-'.$order->pay_price);
        if(UserWalletModel::where('member_id',$user_id)->update($data)){
            $params = [
                'money_type'=>1,
                'change_type'=>40,
                'before_money'=>$wallet->money1,
            ];
            AccountFlowModel::init()->createFlow($user_id,$order->pay_price,0,$params);
            return true;
        }
        $this->errorMsg ='支付失败';
        return false;
    }

    public function payMoney2($user_id,$order)
    {
        $wallet = UserWalletModel::find($user_id);
        if($wallet->money2-$order->pay_price<0){
            $this->errorMsg =get_currency()['money2'].'余额不足';
            return false;
        }

        $data['money2'] = DB::raw('`money2`-'.$order->pay_price);
        if(UserWalletModel::where('member_id',$user_id)->update($data)){
            $params = [
                'money_type'=>2,
                'change_type'=>40,
                'before_money'=>$wallet->money2,
            ];
            AccountFlowModel::init()->createFlow($user_id,$order->pay_price,0,$params);
            return true;
        }

        $this->errorMsg ='支付失败';
        return false;
    }

    public function payMoney3($user_id,$order)
    {
        $wallet = UserWalletModel::find($user_id);
        if($wallet->money3-$order->pay_price<0){
            $this->errorMsg =get_currency()['money3'].'余额不足';
            return false;
        }

        $data['money3'] = DB::raw('`money3`-'.$order->pay_price);
        if(UserWalletModel::where('member_id',$user_id)->update($data)){
            $params = [
                'money_type'=>3,
                'change_type'=>40,
                'before_money'=>$wallet->money3,
            ];
            AccountFlowModel::init()->createFlow($user_id,$order->pay_price,0,$params);
            return true;
        }

        $this->errorMsg ='支付失败';
        return false;
    }

    public function payMoney4($user_id,$order)
    {
        $wallet = UserWalletModel::find($user_id);
        if($wallet->money4-$order->pay_price<0){
            $this->errorMsg =get_currency()['money4'].'余额不足';
            return false;
        }

        $data['money4'] = DB::raw('`money4`-'.$order->pay_price);
        if(UserWalletModel::where('member_id',$user_id)->update($data)){
            $params = [
                'money_type'=>4,
                'change_type'=>40,
                'before_money'=>$wallet->money4,
            ];
            AccountFlowModel::init()->createFlow($user_id,$order->pay_price,0,$params);
            return true;
        }

        $this->errorMsg ='支付失败';
        return false;
    }

    public function payMoney12($user_id,$order)
    {
        $wallet = UserWalletModel::find($user_id);

        $pay_money1 = $order->pay_price-$order->pay_xf_points;

        $pay_money2 = $order->pay_xf_points;

        if($pay_money1>0){

            if($wallet->money1-$pay_money1<0){
                $this->errorMsg =get_currency()['money1'].'余额不足';
                return false;
            }
            $data['money1'] = DB::raw('`money1`-'.$pay_money1);
        }

        if($pay_money2>0){
            if($wallet->money2-$pay_money2<=0){
                $this->errorMsg =get_currency()['money2'].'余额不足';
                return false;
            }
            $data['money2'] = DB::raw('`money2`-'.$pay_money2);
        }

        if(!isset($data)){
            $this->errorMsg = '账户余额不足';
            return false;
        }

        if(UserWalletModel::where('member_id',$user_id)->update($data)){
            if($pay_money1>0){

                $params = [
                    'money_type'=>1,
                    'change_type'=>40,
                    'before_money'=>$wallet->money1,
                ];
                AccountFlowModel::init()->createFlow($user_id,$pay_money1,0,$params);
            }
            if($pay_money2>0){
                $params = [
                    'money_type'=>2,
                    'change_type'=>40,
                    'before_money'=>$wallet->money2,
                ];
                AccountFlowModel::init()->createFlow($user_id,$pay_money2,0,$params);
            }

            return true;
        }

        $this->errorMsg ='支付失败';
        return false;
    }


    public function onlinePay($user_id,$order){

        $this->award($user_id,$order->pay_price,$order->goods_zone);
        return true ;
    }


    private function award($new_uid,$money,$zone,$area_address,$dan=0)
    {
        $group=BonusRecordModel::max('group');
        if(!$group) {
            $group=1;
        }else{
            $group =$group+1;
        }
        ShoppingBonus::make()->setGroup($group)->award($new_uid,$money,$zone,$area_address,$dan);
    }
}
